<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
// use app\admin\Model\Gkdata;
class Userinfo extends Base
{
    // 搜索名称查用户
	public function search(){
        $request=new request();
        $sname=$request->post('sname');
        $data=Db::table('userinfo')->where('nickName','like','%'.$sname.'%')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function index()
    {
        $data=Db::table('userinfo')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入添加用户页面
    public function addpage(){
        return $this->fetch();
    }
    // 添加用户
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $adxm = $request->post("adxm");
        $data=Db::name('admin')->where('adxm',$adxm)->find();
        if(empty($data)){
            $phone = $request->post("phone");
            $password = $request->post("password");
            $nc = $request->post("nc");
            $adgrade = $request->post("adgrade");
            $sex = $request->post("sex");
            // 实例化上传图片的对象
            $upimgobj=controller('Common');
            $image_error = $_FILES['adimg']['error'];
            if ($image_error == 4) {
                $adimg='';
            }else{
                $file=$request->file('adimg');
                $adimg=$upimgobj->upaction1($file);          
            }
            $data1=[
                "adxm"=>$adxm,
                "adpd"=>$password,
                "adphone"=>$phone,
                "adname"=>$nc,
                "adgrade"=>$adgrade,
                "adimg"=>$adimg,
                "adsex"=>$sex
            ];
            $re=Db::name('admin')->insert($data1);
            if($re){
                $this->success('新增用户成功', 'admin/index');
            }else{
                return $this->error('新增用户失败');
            }
            
            
        }else{
            // return $this->error('用户名已存在，请重新输入！');
            var_dump($data);
        }
    }
    // 进入更改
    public function editpage(){
        $request=new request();
        $uid= intval($request->get("id"));
        $data=Db::name('userinfo')->where('uid',$uid)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 更改用户
    public  function edituser(){
        $request=new request();
        $data1=[];
        $uid=$request->post('uid');
        $data1['uid']=$uid;
        $data=Db::name('userinfo')->where('uid',$uid)->find();
        $phone = $request->post("phone");
        if($phone!=$data['phone']){
            $data1['phone']=$phone;
        }
        $da=Db::name('userinfo')->update($data1);
        if($da){
            $this->success('更新用户成功', 'userinfo/index');
        }else{
            return $this->error('更新用户失败');
        }

    }
    // 删除用户
    public function deluserinfo()
    {

        $request=new request();
        $uid=$request->get("id");
        $da=Db::name('userinfo')->delete($uid);
        if($da){
            $this->success('删除成功', 'userinfo/index');
        }
        else{
            return $this->error('删除失败');
        }
    }




}
